package com.edgclub.edg.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.edgclub.edg.R;
import com.edgclub.edg.adapter.adapteruitls.GzBaseAdapter;
import com.edgclub.edg.adapter.adapteruitls.MyItemClickListener;
import com.edgclub.edg.adapter.adapteruitls.MyItemLongClickListener;
import com.edgclub.edg.adapter.adapteruitls.RViewHolder;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.model.UserBean;
import com.rockerhieu.emojicon.EmojiconTextView1;

import java.util.List;

/**
 */
public class ChatBlacklistAdapter extends GzBaseAdapter{
    private List<UserBean> datas = null;
    private Context context;
    private GlideUtils glideUtils;
    public ChatBlacklistAdapter(Context context) {
        this.context = context;
        glideUtils = new GlideUtils();
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ChatBlacklistAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_message_chatblack,viewGroup,false);
        ViewHolder vh = new ViewHolder(view,mListener,mLongClickListener);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ChatBlacklistAdapter.ViewHolder viewHolder= (ViewHolder) holder;
        final UserBean user=datas.get(position);
        glideUtils.Glidde_loadCropCircleImage(context,user.getLogo(),viewHolder.mUser_icon);
        viewHolder.name.setText(user.getNick());
        viewHolder.mess.setText(user.getIntro());
        viewHolder.mess_nosee.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toggleBlockGroup(false,user);
            }
        });
    }


    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }

    public List<UserBean> getDatas() {
        return datas;
    }

    public void setDatas(List<UserBean> datas) {
        this.datas = datas;
    }




    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RViewHolder {
        private ImageView mUser_icon;
        private TextView name;
        private EmojiconTextView1 mess;
        private TextView mess_nosee;
        public ViewHolder(View view, MyItemClickListener listener, MyItemLongClickListener longClickListener){
            super(view, listener, longClickListener);
            mUser_icon = (ImageView) view.findViewById(R.id.user_icon);
            name = (TextView) view.findViewById(R.id.name);
            mess = (EmojiconTextView1) view.findViewById(R.id.peoplejs);
            mess_nosee = (TextView) view.findViewById(R.id.mess_nosee);
        }
    }
    private void toggleBlockGroup(boolean res,final UserBean user) {
        if(!res){
            showDialog(context,context.getResources().getString(R.string.Is_unblock),false);
            new Thread(new Runnable() {
                public void run() {
                    try {
//                        if(chatBean1.getChatType()== Constant.CHATTYPE_GROUP)
//                            EMClient.getInstance().groupManager().unblockGroupMessage(chatBean1.getImid());
//                        else
                            EMClient.getInstance().contactManager().removeUserFromBlackList(user.getImid());
                        ((Activity)context).runOnUiThread(new Runnable() {
                            public void run() {
                                dismissDialog();
                                datas.remove(user);
                                notifyDataSetChanged();
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                        ((Activity)context).runOnUiThread(new Runnable() {
                            public void run() {
                                dismissDialog();
                                Toast.makeText(context,context.getResources().getString( R.string.remove_group_of), Toast.LENGTH_SHORT).show();
                            }
                        });

                    }
                }
            }).start();

        } else {
            showDialog(context,context.getResources().getString(R.string.group_is_blocked),false);
            final String st9 = context.getResources().getString(R.string.group_of_shielding);
            new Thread(new Runnable() {
                public void run() {
                    try {
//                        if(chatBean1.getChatType()==Constant.CHATTYPE_GROUP)
//                            EMClient.getInstance().groupManager().blockGroupMessage(chatBean1.getImid());
//                        else
                            EMClient.getInstance().contactManager().addUserToBlackList(user.getImid(),true);
                        ((Activity)context).runOnUiThread(new Runnable() {
                            public void run() {
                                dismissDialog();
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                        ((Activity)context).runOnUiThread(new Runnable() {
                            public void run() {
                                dismissDialog();
                                Toast.makeText(context, st9, Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                }
            }).start();
        }
    }
}
